package com.letv.whatslive.mongo.dao;

import com.google.common.collect.Lists;
import com.letv.whatslive.common.utils.ObjectUtils;
import com.letv.whatslive.model.DoubleLiveLog;
import com.letv.whatslive.model.convert.DoubleLiveLogConvert;
import com.letv.whatslive.mongo.BaseDAO;
import com.mongodb.BasicDBObject;
import com.mongodb.DBCursor;
import com.mongodb.DBObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.Map;

/**
 * Created by wangjian on 16-1-28.
 */
@Repository
public class DoubleLiveLogDAO extends BaseDAO {

    private final static Logger logger = LoggerFactory.getLogger(DoubleLiveLogDAO.class);

    protected String collectionName = "doubleLiveLog";

    @Override
    protected void init() {
        super.init(this.collectionName);
    }

    @Override
    protected long getAutoIncrementId() {
        return idGenerate.generateId(this.collectionName);
    }

    /**
     * 插入双人直播消息日志信息
     * @param doubleLiveLog
     */
    public long insertDoubleLiveLog(DoubleLiveLog doubleLiveLog){
        DBObject obj = DoubleLiveLogConvert.castDoubleLiveLogToDBObject(doubleLiveLog);
        return this.insert(obj);
    }

    /**
     * 查询活动的记录数
     * @param params 查询条件参数
     * @return 满足条件的日志记录数
     */
    public Long countDoubleLiveLogByParams(Map params) {
        //dbObj 类似于 mysql 查询中的 where 关键字，可以添加各种搜索条件
        BasicDBObject query = getBasicDBObjectByParams(params);
        return this.dbCollection.count(query);
    }

    /**
     * 查询活动列表
     * @param orders 查询的排序条件
     * @param params 模糊查询的查询条件
     * @param start  查询条件的开始分页数
     * @param limit  查询条件的分页每页条数
     * @return
     */
    public List<DoubleLiveLog> getDoubleLiveLogListByParams(Map<String, Object> orders, Map<String, Object> params, Integer start, Integer limit) {

        List<DoubleLiveLog> result = Lists.newArrayList();
        if (limit == null) {
            limit = DEFAULT_NUM;
        }
        try {
            //dbObj 类似于 mysql 查询中的 where 关键字，可以添加各种搜索条件
            BasicDBObject query = getBasicDBObjectByParams(params);
            BasicDBObject order = getOrderParams(orders);
            Integer skip = start == null ? 0 : start;
            DBCursor cur = this.dbCollection.find(query).skip(skip)
                    .sort(order).limit(limit);
            while (cur.hasNext()) {
                DBObject dbObject = cur.next();
                DoubleLiveLog doubleLiveLog = DoubleLiveLogConvert.castDBObjectToDoubleLiveLog(dbObject);
                result.add(doubleLiveLog);
            }
        } catch (Exception e) {
            logger.error("",e);
        }
        return result;
    }

    /**
     * 根据查询参数Map获取查询条件对象
     * @param params 查询参数Map
     * @return
     */
    private BasicDBObject getBasicDBObjectByParams(Map params){
        //dbObj 类似于 mysql 查询中的 where 关键字，可以添加各种搜索条件
        BasicDBObject query = new BasicDBObject();// 新建查询基类对象 dbo
        if (params != null && params.size() > 0) {
            if (params.get("uid") != null) {
                query.put("uid", ObjectUtils.toString(params.get("uid")));
            }
            if (params.get("inviteUid") != null) {
                query.put("inviteUid", ObjectUtils.toString(params.get("inviteUid")));
            }
            if (params.get("pId") != null) {
                query.put("pId", ObjectUtils.toString(params.get("pId")));
            }
            if (params.get("action") != null) {
                query.put("action", ObjectUtils.toInteger(params.get("action")));
            }
            if (params.get("status") != null) {
                query.put("status", ObjectUtils.toInteger(params.get("status")));
            }
        }
        return query;
    }

    /**
     * 获取排序的BasicDBObject对象
     * @param orders
     * @return
     */
    private BasicDBObject getOrderParams(Map<String, Object> orders) {
        BasicDBObject order = new BasicDBObject();
        if(null != orders &&orders.size()>0){
            for(String key : orders.keySet()){
                order.append(key, Integer.valueOf(String.valueOf(orders.get(key))));
            }
        }else{
            //默认按照创建时间排序
            order.append("date", 1);
        }
        return order;
    }

}
